
var that, go, page = 1, util = require('../../utils/helper.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_new_user: true,
    time: '',
    list: [],
    start_time: '',
    swiperIndex: 0,
    str_time: '',
    end_time: '',
    id: 0,
    easing: 'easeInOutCubic',
    Insufficient: false,
    hours: '',
    minutes: '',
    seconds: '',
    totalCount: '0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    var time = util.formatDate(new Date());
    page = 1
    console.log(time)
    that.user_status()
    that.supindex(options.id)
    that.setData({
      id: options.id,
      time: time
    })
    getApp().page.onLoad(this, options)
  },

  onShow: function (e) {
    getApp().page.onShow(this)
  },
  onReady: function (e) {

  },
  user_status: function (e) {
   var o = this
    getApp().request({
      url: getApp().api.default.user_status,
      success: function (e) {
        o.setData({
          is_new_user: e.data.is_new_user
        })
      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },
  onReachBottom: function (t) {
    var a = this
    if (a.data.totalCount == a.data.list.length) {
      getApp().core.showToast({
        title: "没有更多啦",
        icon: "none"
      })
    } else {
      getApp().page.onReachBottom(this);
      page += 1
      this.supindex(a.data.id);
    }
  },

  supindex: function (e) {
    var list = that.data.list
    getApp().request({
      url: getApp().api.surprise.exchange_index,
      data: {
        aid: e,
        page: page
      },
      success: function (t) {
        console.log(t)
        if (t.code == 0) {
          for (var i = 0; i < t.data.list.length; i++) {
            t.data.list[i].exchange_attr = JSON.parse(t.data.list[i].exchange_attr)
            list.push(t.data.list[i])
          }
          console.log('list', list)
          that.setData({
            list: list,
            totalCount: t.data.pagination.totalCount,
            start_time: t.data.setting.start_time,
            end_time: t.data.setting.end_time
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '本轮活动已结束，请期待下轮活动',
            showCancel: !1,
            confirmText: "确认",
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }

      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },

  goodsInfo: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.goodsId
    var goods_num = e.currentTarget.dataset.goods_num
    var lgs_id = e.currentTarget.dataset.lgs_id
    if (!that.data.is_new_user) {
      getApp().core.showModal({
        title: '您还未成为99会员，点击确认即可成为99会员。',
        success: function (e) {
          e.confirm && (wx.navigateTo({
            url: '/pages/Nineactivities/Nineactivities',
          }))
        }
      })
      return;
    }
    if (goods_num == '0') {
      getApp().core.showToast({
        title: "此商品已售罄",
        icon: "none"
      })
    } else {
      console.log(that.data.time < that.data.start_time)
      if (that.data.time < that.data.start_time) {
        getApp().core.showToast({
          title: "活动未开始，开始时间为" + that.data.start_time,
          icon: "none"
        })
      } else {
        if (that.data.time > that.data.end_time) {
          getApp().core.showToast({
            title: "活动已结束",
            icon: "none"
          })
        } else {
          wx.navigateTo({
            url: '/pages/exchange-details/exchange-details?goods_id=' + id + '&aid=' + that.data.id + '&lgs_id=' + lgs_id,
          })
        }
      }
    }

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(go);
  },
  getDate(endDateStr) {
    //结束时间
    go = setInterval(function () {
      var endDate = new Date(endDateStr);
      //当前时间
      var nowDate = new Date();
      //相差的总秒数
      var totalSeconds = parseInt((endDate - nowDate) / 1000);
      //天数
      var days = Math.floor(totalSeconds / (60 * 60 * 24));
      //取模（余数）
      var modulo = totalSeconds % (60 * 60 * 24);
      //小时数
      var hours = Math.floor(modulo / (60 * 60)) < 10 ? '0' + Math.floor(modulo / (60 * 60)) : Math.floor(modulo / (60 * 60));
      var hours1 = Math.floor(modulo / (60 * 60));
      modulo = modulo % (60 * 60);
      //分钟
      var minutes = Math.floor(modulo / 60) < 10 ? '0' + Math.floor(modulo / 60) : Math.floor(modulo / 60);
      var minutes1 = Math.floor(modulo / 60);

      //秒
      var seconds = modulo % 60 < 10 ? '0' + modulo % 60 : modulo % 60;
      var seconds1 = modulo % 60;
      //输出到页面
      var str_time = '距离活动结束还有' + hours1 + "小时" + minutes1 + "分钟" + seconds1 + "秒"
      // var str_time = '距离活动结束还有'+ days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒"
      that.setData({
        str_time: str_time,
        hours: hours,
        minutes: minutes,
        seconds: seconds
      })
      if (days == 0 && hours == 0 && minutes == 0 && seconds == 0) {
        wx.showModal({
          title: '提示',
          content: '活动已结束',
          showCancel: !1,
          confirmText: "确认",
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      }
      // console.log("还剩:" + days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒")
    }, 1000)
  },

})